package com.ztmg.water.service.recycle.impl;

import com.ztmg.common.util.DateUtil;
import com.ztmg.water.mapper.student.StudentTicketMapper;
import com.ztmg.water.model.recycle.TicketRecycleRecord;
import com.ztmg.water.model.student.StudentTicket;
import com.ztmg.water.vo.recycle.TicketRecycleRecordAllVO;
import com.ztmg.water.vo.recycle.TicketRecycleRecordVO;
import com.ztmg.water.dto.recycle.TicketRecycleRecordDTO;
import com.ztmg.water.dto.recycle.TicketRecycleRecordPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.ztmg.water.service.recycle.TicketRecycleRecordService;
import com.ztmg.water.mapper.recycle.TicketRecycleRecordMapper;import com.ztmg.water.model.recycle.TicketRecycleRecordExample;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TicketRecycleRecordServiceImpl implements TicketRecycleRecordService{

    @Resource
    private TicketRecycleRecordMapper ticketRecycleRecordMapper;
    @Resource
    private StudentTicketMapper  studentTicketMapper;

    @Override
    public JsonResult<TicketRecycleRecordAllVO> selectTicketRecycleById(Integer id) {
        if(null != id){
            TicketRecycleRecordAllVO resultObj =  ticketRecycleRecordMapper.selectTicketRecycleById(id);
            return JsonResult.success(resultObj);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult create(TicketRecycleRecordDTO dto) {
        if(null != dto){
            TicketRecycleRecord bean = new TicketRecycleRecord();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            ticketRecycleRecordMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(TicketRecycleRecordDTO dto) {
        if(null != dto && null != dto.getId()){
            TicketRecycleRecord bean = new TicketRecycleRecord();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            ticketRecycleRecordMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public JsonResult updateRecordStatus(TicketRecycleRecordDTO dto) {
        if(null != dto && null != dto.getId()){
            TicketRecycleRecord bean = new TicketRecycleRecord();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            ticketRecycleRecordMapper.updateByPrimaryKeySelective(bean);
            //判断审核是否成功
            if(bean.getStatus()==3)
            {
                TicketRecycleRecord resultDetail = ticketRecycleRecordMapper.selectByPrimaryKey(bean.getId());
                //给当前学生添加对应的水票信息
                Map<String,String> params =  new HashMap<>(2);
                params.put("studentId",String.valueOf(bean.getStudentId()));
                params.put("ticketId",String.valueOf(resultDetail.getTicketId()));
                int nums = studentTicketMapper.selectCountByParams(params);
                if(nums>0)
                {
                    params.put("ticketNum",String.valueOf(resultDetail.getTicketNum()));
                    params.put("modifier","");
                    params.put("modifyTime",DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
                    studentTicketMapper.updateTicketNums(params);
                }else
                {
                    StudentTicket studentTicket = new StudentTicket();
                    studentTicket.setCreater("");
                    studentTicket.setCreateTime(new Date());
                    studentTicket.setModifier("");
                    studentTicket.setModifyTime(new Date());
                    studentTicket.setRemark("");
                    studentTicket.setStatus(1);
                    studentTicket.setStudentId(resultDetail.getStudentId());
                    studentTicket.setTicketId(resultDetail.getTicketId());
                    studentTicket.setTicketNum(resultDetail.getTicketNum());
                    studentTicketMapper.insertSelective(studentTicket);
                }

            }
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<TicketRecycleRecordVO> selectById(Integer id) {
        if(null != id){
            TicketRecycleRecord bean = ticketRecycleRecordMapper.selectByPrimaryKey(id);
            TicketRecycleRecordVO vo = new TicketRecycleRecordVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<TicketRecycleRecordVO> select(TicketRecycleRecordDTO dto) {
        TicketRecycleRecordExample example = new TicketRecycleRecordExample();
        TicketRecycleRecordExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<TicketRecycleRecord> sysUserList = ticketRecycleRecordMapper.selectByExample(example);
        TicketRecycleRecordVO vo = new TicketRecycleRecordVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<TicketRecycleRecordVO>> selectPage(TicketRecycleRecordPageDTO dto) {
        TicketRecycleRecordExample example = new TicketRecycleRecordExample();
        TicketRecycleRecordExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<TicketRecycleRecord> beanList = ticketRecycleRecordMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<TicketRecycleRecordVO> respDTOS = beanList.parallelStream().map((bean) -> {
            TicketRecycleRecordVO vo = new TicketRecycleRecordVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult<PageInfo<TicketRecycleRecordAllVO>> selectListAllPage(TicketRecycleRecordPageDTO dto) {
        /***
         * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<TicketRecycleRecordAllVO> beanList = ticketRecycleRecordMapper.selectAllList(dto);
        PageInfo pageInfo = new PageInfo<>(beanList);
        pageInfo.setList(beanList);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( ticketRecycleRecordMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(TicketRecycleRecordDTO dto) {
        TicketRecycleRecordExample example = new TicketRecycleRecordExample();
        TicketRecycleRecordExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(ticketRecycleRecordMapper.deleteByExample(example));
    }
}